package com.nuanshui.heatedloan.module.builder;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.nuanshui.heatedloan.constant.TransactionManagerType;
import com.nuanshui.heatedloan.module.facade.IModuleBuilder;
import com.nuanshui.heatedloan.module.features.IFeatureManager;
import com.nuanshui.heatedloan.module.model.Feature;
import com.nuanshui.heatedloan.module.model.FeatureForm;
import com.nuanshui.heatedloan.module.model.Module;
import com.nuanshui.heatedloan.module.model.ModuleForm;

@Service("fakeModuleBuilder")
@Transactional(readOnly = true, value = TransactionManagerType.MODULE_TRANSACTION_MANAGER)
public class FakeModuleBuilder implements IModuleBuilder {

    @Autowired
    private IFeatureManager featureManager;

    @Override
    public Object getResult(ModuleForm moduleForm) {
        //自己组装一个module数据，返回给前台
        Module module = new Module();
        Feature item = createFeatureByModuleName(moduleForm);

        module.setName(item.getName());

        List<Feature> list = new ArrayList<Feature>();

        //处理数据
        FeatureForm form = new FeatureForm();
        form.setModule(module);
        form.setPager(moduleForm.getPager());
        form.setItem(item);
        form.setModuleForm(moduleForm);
        featureManager.handleFeature(form);
        list.add(item);
        module.setList(list);
        module.setNoMoreData((item.getModuleName() != null && item.getModuleName().equals("museumList")) ? 0 :1);
        return module;
    }

    private Feature createFeatureByModuleName(ModuleForm moduleForm) {
        Feature item = new Feature();

        switch (moduleForm.getModuleName()) {
            case "information":
                item.setModuleId(101);//这个是给特殊的media来处理的，所以直接用101
                item.setModuleName(moduleForm.getModuleName());
                item.setName(moduleForm.getName());//用来区分是获取预约指南，还是展馆详情
                break;
            case "MyStoreUp":
                item.setModuleId(19);
                item.setModuleName("myFavorite");
                break;
            case "museumList":
                item.setModuleId(104);
                item.setModuleName(moduleForm.getModuleName());
                break;
        }

        return item;
    }

}
